package org.example.myleet.p817;

import org.example.myleet.Utils.ListNode;

import java.util.HashSet;
import java.util.Set;

public class Solution {
    public int numComponents(ListNode head, int[] nums) {
        Set<Integer> numbers = new HashSet<>(nums.length);
        for (int num : nums) {
            numbers.add(num);
        }
        int count = 0;
        boolean started = false;
        ListNode cur = head;
        while (null != cur) {
            if (numbers.contains(cur.val)) {
                if (!started) {
                    started = true;
                }
                numbers.remove(cur.val);
            } else {
                if (started) {
                    ++count;
                    started = false;
                }
            }
            cur = cur.next;
        }
        if (started) {
            ++count;
        }
        return count;
    }
}
